package through;

/**
 * @author zhangjiayuan@qipeipu.com
 * @date 2019/1/6 13:28
 * @since 1.0.0
 */
public class Violence {
    public static int kmp1(String ts,String ps){
        char[] tsChar = ts.toCharArray();
        char[] psChar = ps.toCharArray();
        int i = 0;
        int j = 0;
        while(i<tsChar.length && j<psChar.length){
            if(tsChar[i] == psChar[j]){
                i++;
                j++;
            }else{
                i = i-j+1;
                j = 0;
            }
        }
        if(j == psChar.length){
            return i-j;
        }else{
            return -1;
        }
    }

    public static void main(String[] args) {
        int result = Violence.kmp1("abcdefg","cde");
        System.out.println(result);
    }
}
